home *** CD-ROM | disk | FTP | other *** search
/ Flash MX Savvy / FlashMX Savvy.iso / pc / MAC / Amapi3D / Amapi3DTrial_Edition / 3SPACE / TSObject.js < prev    next >
Encoding:
Text File  |  2001-02-20  |  9.9 KB  |  482 lines  |  [AMAS/AMAP]

  1. // -* TSObject.js *-
  2. //
  3. // Description: Functions for accessing and building 3Space objects
  4. // Author:
  5. // Version: $Id: TSObject.js,v 1.7 2000/10/10 09:22:08 consumer Exp $
  6. //
  7.  
  8. //
  9. // Node
  10. //
  11.  
  12. function nodeGetFromID(id)
  13. {
  14.   var node = gXMLObject.nodeFromID(id);
  15.   if (node == null) {
  16.     error("Node '" + id + "' not found!");
  17.   }
  18.  
  19.   return node;
  20. }
  21.  
  22. function nodeGetID(node)
  23. {
  24.   return nodeObjectFindAttribute(node, 'id');
  25. }
  26.  
  27. function makeNode(name, id)
  28. {
  29.   //var node = gXMLObject.createNode(1, name, "x-schema:3Space/Schemas.xml");
  30.   //node.setAttribute('id', id);
  31.  
  32.   //return node;
  33.   
  34. }
  35.  
  36. /*function nodeUpdate(id)
  37.   {
  38.   gPlayer.UpdateXMLNode(id);
  39.   }
  40. */
  41. function updateNode (nodeId) {
  42.     gPlayer.UpdateXMLNode (nodeId) ;
  43. }
  44.  
  45. function updateNodeAttribute (nodeId, attributeName, attributeValue) {
  46.     gPlayer.UpdateXMLNodeAttr (nodeId, attributeName, attributeValue) ;
  47. }
  48.  
  49. function nodeGetAttribute(id, attribute)
  50. {
  51.   var attr = gPlayer.GetAttribute(id, attribute);
  52.   if (attr == "") {
  53.     error("Attribute '" + id + "' not found!");
  54.   }
  55.  
  56.   return attr;
  57. }
  58.  
  59. /*
  60.   function nodeUpdateAttribute(id, attribute, value)
  61.   {
  62.   //alert("Update attribute: { '" + id + "'; '" + attribute + "'; '" + value + "' }");
  63.   gPlayer.UpdateXMLNodeAttr(id, attribute, value);
  64.   }
  65. */
  66.  
  67. /*function nodeAppendChild(id, child)
  68.   {
  69.   nodeGetFromID(id).appendChild(child);
  70.   gPlayer.updateXMLNode(nodeGetID(child));
  71.   }*/
  72.  
  73. function appendChild (parentId, nodeId) {
  74.     gPlayer.appendChild (parentId, nodeId) ;
  75. }
  76.  
  77. /*function nodeRemove(id)
  78.   {
  79.   gPlayer.removeXMLNode(id);
  80.   }
  81. */
  82. function removeNode (nodeId) {
  83.     gPlayer.removeXMLNode (nodeId) ;
  84. }
  85.  
  86. function nodeObjectFindAttribute(node, attributeName)
  87. {
  88.   var attributes = node.attributes;
  89.  
  90.   if (attributes.length != 0) {
  91.     for (i = 0; i < attributes.length; i++) {
  92.       var attrNode = attributes.item(i);
  93.       if (attrNode.nodeName == attributeName)
  94.     return attrNode.value;
  95.     }
  96.   }
  97.   return null;
  98. }
  99.  
  100. function nodeFindAttribute(id, attributeName)
  101. {
  102.   return nodeObjectFindAttribute(nodeGetFromID(id), attribute);
  103. }
  104.  
  105. function nodeFindFirstChild(id, childName)
  106. {
  107.   var nodeList = nodeGetFromID(id).childNodes;
  108.  
  109.   if (nodeList.length == 0)
  110.     return null;
  111.  
  112.   for (i = 0; i < nodeList.length; i++) {
  113.     var childNode = nodeList.item(i);
  114.     if (childNode.nodeName == childName)
  115.       return nodeGetID(childNode);
  116.   }
  117. }
  118.  
  119. //
  120. // Basic solid
  121. //
  122.  
  123. function makeSolid(id, fixed, position)
  124. {
  125.   var solid = makeNode("Solid", id);
  126.  
  127.   solid.setAttribute('fixed', fixed);
  128.   solid.setAttribute('position', position);
  129.  
  130.   return solid;
  131. }
  132.  
  133. function solidGetPosition(solidName)
  134. {
  135.   return makePointFromString(nodeGetAttribute(solidName, 'position'));
  136. }
  137.  
  138. function solidGetBoundingBox(solidName)
  139. {
  140.   return makeBoundingBoxFromString(nodeGetAttribute(solidName, 'boundingBox'));
  141. }
  142.  
  143. function solidGetLengthX(solidName)
  144. {
  145.   return boundingBoxGetLengthX(solidGetBoundingBox(solidName));
  146. }
  147.  
  148. function solidGetLengthY(solidName)
  149. {
  150.   return boundingBoxGetLengthY(solidGetBoundingBox(solidName));
  151. }
  152.  
  153. function solidGetLengthZ(solidName)
  154. {
  155.   return boundingBoxGetLengthZ(solidGetBoundingBox(solidName));
  156. }
  157.  
  158. //
  159. // Basic geometry
  160. //
  161.  
  162. function makeBlock(id, color, lengthX, lengthY, lengthZ)
  163. {
  164.   var block = makeNode("GeomBlock", id);
  165.  
  166.   block.setAttribute('color', color);
  167.   block.setAttribute('lengthX', lengthX);
  168.   block.setAttribute('lengthY', lengthY);
  169.   block.setAttribute('lengthZ', lengthZ);
  170.  
  171.   return block;
  172. }
  173.  
  174. function makeCone(id, length, radius, color)
  175. {
  176.   var cone = makeNode("GeomCone", id);
  177.  
  178.   cone.setAttribute('color', color);
  179.   cone.setAttribute('length', length);
  180.   cone.setAttribute('radius', radius);
  181.  
  182.   return cone;
  183. }
  184.  
  185. function makeCube(id, length, color)
  186. {
  187.   var cube = makeNode("GeomCube", id);
  188.  
  189.   cube.setAttribute('color', color);
  190.   cube.setAttribute('length', length);
  191.  
  192.   return cube;
  193. }
  194.  
  195. function makeCylinder(id, length, radius, color)
  196. {
  197.   var cylinder = makeNode("GeomCylinder", id);
  198.  
  199.   cylinder.setAttribute('color', color);
  200.   cylinder.setAttribute('length', length);
  201.   cylinder.setAttribute('radius', radius);
  202.  
  203.   return cylinder;
  204. }
  205.  
  206. function makePlan(id, length, width, color)
  207. {
  208.   var plan = makeNode("GeomPlan", id);
  209.  
  210.   plan.setAttribute('color', color);
  211.   plan.setAttribute('length', length);
  212.   plan.setAttribute('width', width);
  213.  
  214.   return plan;
  215. }
  216.  
  217. function makeSphere(id, radius, color)
  218. {
  219.   var sphere = makeNode("GeomSphere", id);
  220.  
  221.   sphere.setAttribute('color', color);
  222.   sphere.setAttribute('radius', radius);
  223.  
  224.   return sphere;
  225. }
  226.  
  227. function makeText(id, text, size, color)
  228. {
  229.   var text = makeNode("GeomText", id);
  230.  
  231.   text.setAttribute('color', color);
  232.   text.setAttribute('text', text);
  233.   text.setAttribute('text', size);
  234.  
  235.   return text;
  236. }
  237.  
  238. //
  239. // Light
  240. //
  241.  
  242. function makeSolidPointLight(id, position, color, state)
  243. {
  244.   var light = makeNode("SolidPointLight", id);
  245.  
  246.   light.setAttribute('fixed', '1');
  247.   light.setAttribute('position', position);
  248.   light.setAttribute('color', color);
  249.   light.setAttribute('state', state);
  250.  
  251.   return light;
  252. }
  253.  
  254. function makeSolidSpotLight(id, position, direction, cutOffAngle, dropOffAngle, color, state)
  255. {
  256.   var light = makeNode("SolidSpotLight", id);
  257.  
  258.   light.setAttribute('fixed', '1');
  259.   light.setAttribute('position', position);
  260.   light.setAttribute('direction', direction);
  261.   light.setAttribute('cutOffAngle', cutOffAngle);
  262.   light.setAttribute('dropOffAngle', dropOffAngle);
  263.   light.setAttribute('color', color);
  264.   light.setAttribute('state', state);
  265.  
  266.   return light;
  267. }
  268.  
  269. //
  270. // Camera
  271. //
  272.  
  273. function cameraGetPosition(id)
  274. {
  275.   return solidGetPosition(id);
  276. }
  277.  
  278. function cameraGetTargetPosition(id)
  279. {
  280.   return makePointFromString(nodeGetAttribute(id, 'targetPoint'));
  281. }
  282.  
  283. function cameraLookAt(id, targetPoint)
  284. {
  285.   nodeUpdateAttribute(id, 'targetPoint', targetPoint)
  286.     }
  287.  
  288. function cameraLookAtSolid(id, targetID)
  289. {
  290.   nodeUpdateAttribute(id, 'target', targetID);
  291. }
  292.  
  293. //
  294. // Particle systems
  295. //
  296.  
  297. function makeParticleSystem(id,
  298.                 textureFile,
  299.                 position, 
  300.                 rotation, 
  301.                 yawVariation, 
  302.                 pitchVariation,
  303.                 initialColor,
  304.                 endColor,
  305.                 initialSize,
  306.                 endSize,
  307.                 speed,
  308.                 speedVariation,
  309.                 lifeSpan,
  310.                 lifeSpanVariation)
  311. {
  312.   var psystem = makeNode("ParticleSystem");
  313.  
  314.   psystem.setAttribute('id', id);
  315.   psystem.setAttribute('particleTexture', textureFile);
  316.   psystem.setAttribute('position', position);
  317.   psystem.setAttribute('rotation', rotation);
  318.   psystem.setAttribute('yawVariation', yawVariation);
  319.   psystem.setAttribute('pitchVariation', pitchVariation);
  320.   psystem.setAttribute('initialColor', initialColor);
  321.   psystem.setAttribute('endColor', endColor);
  322.   psystem.setAttribute('initialSize', initialSize);
  323.   psystem.setAttribute('endSize', endSize);
  324.   psystem.setAttribute('speed', speed);
  325.   psystem.setAttribute('speedVariation', speedVariation);
  326.   psystem.setAttribute('lifeSpan', lifeSpan);
  327.   psystem.setAttribute('lifeSpanVariation', lifeSpanVariation);
  328.  
  329.   return psystem;
  330. }
  331. /*
  332. function makeBurnParticleSystem(id,
  333.                                 textureFile,
  334.                                 position,
  335.                                 rotation,
  336.                                 intensity,
  337.                                 size)
  338. {
  339.   var yawVar = '5';
  340.   var pitchVar = '5';
  341.   var initialColor = '1 0.5 0';
  342.   var endColor = '1 1 1';
  343.   var initialSize = new String(size);
  344.   var endSize = new String(0.05 * size);
  345.   var speed = new String(0.00005 * size * intensity);
  346.   var speedVariation = new String(0.000005 * size * intensity);
  347.   var lifeSpan = new String(100000 * size * intensity / 100);
  348.   var lifeSpanVariation = new String(1000 * size * intensity / 100);
  349.  
  350.   return makeParticleSystem(id,
  351.                             textureFile,
  352.                             position,
  353.                             rotation,
  354.                             yawVar,
  355.                             pitchVar,
  356.                             initialColor,
  357.                             endColor,
  358.                             initialSize,
  359.                             endSize,
  360.                             speed,
  361.                             speedVariation,
  362.                             lifeSpan, 
  363.                             lifeSpanVariation);
  364. }
  365. */
  366. function makeBurnParticleSystem(id,
  367.                 textureFile,
  368.                 position,
  369.                 rotation,
  370.                 intensity,
  371.                 size)
  372. {
  373.   var yawVar = '5';
  374.   var pitchVar = '5';
  375.   var initialColor = '1 0.5 0';
  376.   var endColor = '1 1 1';
  377.   var initialSize = new String(0.9 * size);
  378.   var endSize = new String(0.3 * size);;
  379.   var speed = '0.01';
  380.   var speedVariation = '0.005';
  381.   var lifeSpan = '800';
  382.   var lifeSpanVariation = '200';
  383.  
  384.   return makeParticleSystem(id,
  385.                 textureFile,
  386.                 position,
  387.                 rotation,
  388.                 yawVar,
  389.                 pitchVar,
  390.                 initialColor,
  391.                 endColor,
  392.                 initialSize,
  393.                 endSize,
  394.                 speed,
  395.                 speedVariation,
  396.                 lifeSpan, 
  397.                 lifeSpanVariation);
  398. }
  399.  
  400. //
  401. // Forces
  402. //
  403.  
  404. function makeDampingSolidForce(id, angular, linear)
  405. {
  406.   var damping = makeNode("DampingSolidForce", id);
  407.  
  408.   damping.setAttribute('angular', angular);
  409.   damping.setAttribute('linear', linear);
  410.  
  411.   return damping;
  412. }
  413.  
  414. function makeSpringForce(id, constant, damping, point, target)
  415. {
  416.   var spring = makeNode("SpringForce", id);
  417.  
  418.   spring.setAttribute('constant', constant);
  419.   spring.setAttribute('damping', damping);
  420.   spring.setAttribute('point', point);
  421.   spring.setAttribute('target', target);
  422.  
  423.   return spring;
  424. }
  425.  
  426. function makeDragForce(id, intensity, dragPoint, targetPoint)
  427. {
  428.   var drag = makeNode("DragForce", id);
  429.  
  430.   drag.setAttribute('intensity', intensity);
  431.   drag.setAttribute('dragPoint', dragPoint);
  432.   drag.setAttribute('targetPoint', targetPoint);
  433.  
  434.   return drag;
  435. }
  436.  
  437. function makeShootForce(id, direction, intensity, point)
  438. {
  439.   var shoot = makeNode("ShootForce", id);
  440.  
  441.   shoot.setAttribute('direction', direction);
  442.   shoot.setAttribute('intensity', intensity);
  443.   shoot.setAttribute('point', point);
  444.  
  445.   return shoot;
  446. }
  447.  
  448. //
  449. // Links
  450. //
  451.  
  452. function makeAxisLink(id, axis, point, target)
  453. {
  454.   var link = makeNode("AxisLink", id);
  455.  
  456.   link.setAttribute('axis', axis);
  457.   link.setAttribute('point', point);
  458.   link.setAttribute('target', target);
  459.  
  460.   return link;
  461. }
  462. function makeCylindricalLink(id, axis, point, target)
  463. {
  464.   var link = makeNode("CylindricalLink", id);
  465.  
  466.   link.setAttribute('axis', axis);
  467.   link.setAttribute('point', point);
  468.   link.setAttribute('target', target);
  469.  
  470.   return link;
  471. }
  472.  
  473. function makeSphericalLink(id, point, target)
  474. {
  475.   var link = makeNode("SphericalLink", id);
  476.  
  477.   link.setAttribute('point', point);
  478.   link.setAttribute('target', target);
  479.  
  480.   return link;
  481. }
  482.